<template>
  <div>
    <el-dialog
      title="编辑"
      :visible.sync="editDialogData.visible"
      :close-on-click-modal="false"
      width="80%"
      top="8vh"
    >
      <el-row>
        <el-col :span="24">
          <el-form :model="dataForm"  :rules="dataRule" label-width="120px">
            <div class="rwfqnew">
              <el-row>
                <el-col :span="24">
                  <el-col :span="12">
                    <el-form-item label="模板名称" prop="mbname">
                      <el-input
                        v-model="dataForm.mbname"
                        @change="changeHead(dataForm.mbname,'mbname',dataForm.id)"
                        placeholder="模板名称"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="描述" prop="description">
                      <el-input
                        v-model="dataForm.description"
                        @change="changeHead(dataForm.description,'description',dataForm.id)"
                        placeholder="描述"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-col>
              </el-row>
            </div>
          </el-form>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-table
            style="width: 100%; margin-top: 20px"
            :data="dataList"
            border
            ref="table"
            v-loading="loading"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column
              type="index"
              width="80"
              header-align="center"
              align="center"
              label="序号"
            ></el-table-column>
            <el-table-column
              prop="pfxm"
              width="330"
              header-align="center"
              align="center"
              label="评分项目"
            >
              <template slot-scope="scope">
                <el-input
                  v-if="!disabled"
                  @change="
                    changeValue(scope.row.pfxm, 'pfxm', scope.row.id)
                  "
                  v-model="scope.row.pfxm"
                  placeholder="评分项目"
                />
                <span v-else>{{ scope.row.pfxm }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="fssz"
              width="332"
              header-align="center"
              align="center"
              label="分数设置"
            >
              <template slot-scope="scope">
                <el-input
                  v-if="!disabled"
                  @change="
                    changeValue(scope.row.fssz, 'fssz', scope.row.id)
                  "
                  v-model="scope.row.fssz"
                  placeholder="分数设置"
                />
                <span v-else>{{ scope.row.fssz }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="pfsm"
              width="330"
              header-align="center"
              align="center"
              label="评分说明"
            >
              <template slot-scope="scope">
                <el-input
                  v-if="!disabled"
                  @change="
                    changeValue(scope.row.pfsm, 'pfsm', scope.row.id)
                  "
                  v-model="scope.row.pfsm"
                  placeholder="评分说明"
                />
                <span v-else>{{ scope.row.pfsm }}</span>
              </template>
            </el-table-column>

            <el-table-column
              label="操作"
              width="60"
              header-align="center"
              align="center"
              v-if="!disabled"
            >
              <template slot="header">
                <el-button
                  type="primary"
                  icon="el-icon-plus"
                  circle
                  @click="addBtn()"

                ></el-button>
              </template>
              <template slot-scope="scope">
                <el-button
                  type="danger"
                  icon="el-icon-minus"
                  circle
                  @click="delBtn(scope.row)"

                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" plain @click="backBtn">关闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

import {
    removeMbsubById,
    updateMbsubById,
    addMbsub,
    updateForMb,
    getzxgzZibiaoPage
} from "@/api/hetongrenwu/zhuanxianggongzuo/zxgzmbcj";
import { effective } from "@/api/hetongrenwu/hetongyeji/hetongnd";
export default {
  components: {

  },
  props: {
      editDialogData: {},
  },
  data() {
      return {
          shouyuerendataList: [],
          dataList: [],
          dataForm: {},
          categoryList: [],
          jsonData: "",
          index: 1,
          id: "",
          zdrwBkhUsers: [],
          loading: false,
          disabled: Boolean,
          dataRule: {
              mbname: [
                  { required: true, message: "请输入模板名称", trigger: "change" },
              ],
              description: [
                  { required: true, message: "请输入描述", trigger: "change" },
              ]
          },
      };
  },
  watch: {
      editDialogData: {
          handler(newVal, oldVal) {
              if (newVal.visible) {
                  console.log(newVal.rows)
                  this.dataForm = newVal.rows;
                  this.fngetDetailListdepid(newVal.rows.id);

                  this.disabled = false;
              }
          },
          immediate: true,
          deep: true,
      },
  },
  created() {
    this.fneffective();
  },
  methods: {
    backBtn() {
        this.dataList = [];
        this.shouyuerendataList = [];
        this.dataForm = {};
        this.editDialogData.visible = false;
        this.$emit("editDialogDataChildren",'返回')
    },
    addBtn() {

        var item = {
            pfxm: "评分项目",
            fssz: 0,
            pfsm: "评分说明",
            pfmbid: this.dataForm.id,
        };
        this.index++;

       addMbsub(item).then((res) => {
            this.$message.closeAll();
            this.$message.success("添加成功");
            this.fngetDetailListdepid(this.dataForm.id);
        });
    },
    delBtn(row) {
        removeMbsubById(row.id).then((res) => {
            this.$message.closeAll();
            this.$message.success("删除成功");
            this.fngetDetailListdepid(this.dataForm.id);
        });
    },
    changeValue(val, type, id) {
      console.log(val, type, id);
      var params = {
        [type]: val,
        id: id,
      };
      updateMbsubById(params).then((res) => {
        this.$message.closeAll();
        this.$message.success("修改成功");
        this.fngetDetailListdepid(this.dataForm.id);
      });
    },
    changeHead(val, type, id){
      var param ={
        [type]: val,
        id: id,
      }
      updateForMb(param).then((res) => {
        this.$message.closeAll();
        this.$message.success("修改成功");
        this.fngetDetailListdepid(this.dataForm.id);
      });
    },
    fneffective() {
      effective({
        tagName: "专项工作",
      }).then((res) => {
        this.categoryList = res.data.data;
      });
    },
    fngetDetailListdepid(pfmbid) {
      this.loading = true;
      getzxgzZibiaoPage({ pfmbid }).then((response) => {
      this.dataList = response.data.data;
      this.loading = false;
      });
    },

    submitfy(data) {
      this.zdrwBkhUsers = [];
      this.shouyuerendataList = [];
      this.shouyuerendataList = data.tableDataUser;
      console.log(JSON.stringify(this.shouyuerendataList));
      var obj = {};
      this.shouyuerendataList.forEach((ele) => {
        var user = {};
        user.userId = ele.userId;
        user.userName = ele.userCode;
        user.ryName = ele.userName;
        user.deptId = ele.deptId;
        user.userDeptName = ele.deptName;
        user.userZw = ele.userZw;
        user.userLx = "总部发起";
        user.userZj = ele.userZj;
        this.zdrwBkhUsers.push(user);
      });
    },
    getNowTime: function () {
      let dateTime;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      dateTime = yy + "-" + mm + "-" + dd;
      return dateTime;
    },
    getNowTime2: function () {
      let dateTime;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 2;
      let dd = new Date().getDate();
      dateTime = yy + "-" + mm + "-" + dd;
      return dateTime;
    },
  },
};
</script>

<style scoped>
.rwfqnew {
  border-radius: 16px;
  -webkit-box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  background: #fbfbfd;
  padding: 10px 30px 30px 30px;
}
</style>
